using System;
using System.IO;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace APP.HELPER.Utils.Shell
{
	public class ShellShortcut : IDisposable
	{
		[Flags]
		public enum SlgpFlags
		{
			SlgpShortpath = 1,
			SlgpUncpriority = 2,
			SlgpRawpath = 4
		}
		[Flags]
		public enum SlrFlags
		{
			SlrNoUi = 1,
			SlrAnyMatch = 2,
			SlrUpdate = 4,
			SlrNoupdate = 8,
			SlrNosearch = 16,
			SlrNotrack = 32,
			SlrNolinkinfo = 64,
			SlrInvokeMsi = 128
		}
		[Guid("000214F9-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[ComImport]
		public interface IShellLink
		{
			void GetPath([MarshalAs(UnmanagedType.LPWStr)] [Out] string pszFile, int cchMaxPath, ref ShellShortcut.Win32FindDATA pfd, ShellShortcut.SlgpFlags fFlags);
			void GetIDList(ref IntPtr ppidl);
			void SetIDList(IntPtr pidl);
			void GetDescription([MarshalAs(UnmanagedType.LPWStr)] [Out] string pszName, int cchMaxName);
			void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
			void GetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] [Out] string pszDir, int cchMaxPath);
			void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
			void GetArguments([MarshalAs(UnmanagedType.LPWStr)] [Out] string pszArgs, int cchMaxPath);
			void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
			void GetHotkey(ref short pwHotkey);
			void SetHotkey(short wHotkey);
			void GetShowCmd(ref int piShowCmd);
			void SetShowCmd(int iShowCmd);
			void GetIconLocation([MarshalAs(UnmanagedType.LPWStr)] [Out] string pszIconPath, int cchIconPath, ref int piIcon);
			void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
			void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
			void Resolve(IntPtr hwnd, ShellShortcut.SlrFlags fFlags);
			void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
		}
		[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
		public struct Win32FindDATA
		{
			public readonly int dwFileAttributes;
            //public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
            //public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
            //public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
			public int nFileSizeHigh;
			public int nFileSizeLow;
			public int dwReserved0;
			public int dwReserved1;
			[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
			public readonly string cFileName;
			[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
			public readonly string cAlternateFileName;
		}
		private const int StringSize = 1024;
		private readonly string _linkPath;
		private ShellShortcut.IShellLink _isl;
		private bool _disposedValue;
		public string Description
		{
			get
			{
				string text = new string(' ', 1024);
				this._isl.GetDescription(text, 1024);
				return ShellShortcut.RemoveNullChar(text);
			}
			set
			{
				this._isl.SetDescription(value);
			}
		}
		public int IconIndex
		{
			get
			{
				int result = 0;
				string text = new string(' ', 260);
				this._isl.GetIconLocation(text, text.Length, ref result);
				return result;
			}
			set
			{
				this._isl.SetIconLocation(this.IconPath, value);
			}
		}
		public string IconPath
		{
			get
			{
				string text = new string(' ', 260);
				int num = 0;
				this._isl.GetIconLocation(text, text.Length, ref num);
				return ShellShortcut.RemoveNullChar(text);
			}
			set
			{
				this._isl.SetIconLocation(value, this.IconIndex);
			}
		}
		public string Path
		{
			get
			{
				ShellShortcut.Win32FindDATA win32FindDATA = default(ShellShortcut.Win32FindDATA);
				string text = new string(' ', 1024);
				this._isl.GetPath(text, 1024, ref win32FindDATA, ShellShortcut.SlgpFlags.SlgpUncpriority);
				return ShellShortcut.RemoveNullChar(text);
			}
			set
			{
				this._isl.SetPath(value);
			}
		}
		public string WorkingDirectory
		{
			get
			{
				string text = new string(' ', 1024);
				this._isl.GetWorkingDirectory(text, 1024);
				return ShellShortcut.RemoveNullChar(text);
			}
			set
			{
				this._isl.SetWorkingDirectory(value);
			}
		}
		public ShellShortcut(string linkPath)
		{
			Type typeFromCLSID = Type.GetTypeFromCLSID(new Guid("00021401-0000-0000-C000-000000000046"));
			this._isl = (ShellShortcut.IShellLink)Activator.CreateInstance(typeFromCLSID);
			this._linkPath = linkPath;
			if (File.Exists(linkPath))
			{
				((IPersistFile)this._isl).Load(linkPath, 0);
			}
		}
		public static string RemoveNullChar(string input)
		{
			int num = input.IndexOf(Convert.ToChar(0));
			return (num < 0) ? input : input.Remove(num);
		}
		public void Save()
		{
			((IPersistFile)this._isl).Save(this._linkPath, true);
		}
		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}
		protected virtual void Dispose(bool disposing)
		{
			if (!this._disposedValue)
			{
				if (disposing)
				{
				}
				if (this._isl != null)
				{
					Marshal.ReleaseComObject(this._isl);
					this._isl = null;
				}
			}
			this._disposedValue = true;
		}
	}
}
